package com.yx1996.test;

import com.yx1996.mapper.OrdersMapper;
import com.yx1996.mapper.UserMapper;
import com.yx1996.pojo.Orders;
import com.yx1996.pojo.QueryVo;
import com.yx1996.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class testMapper {

    @Test
    // 测试mapper根据id查询用户
    public void fun() throws IOException {

        // 加载主配置文件
        InputStream resource = Resources.getResourceAsStream("mybatis.xml");
        // 获得mybatis的sessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        // 获得session
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 获得user的mapper对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.findUserById(10);
        System.out.println(user);
    }

    @Test
    // 测试mapper根据QueryVo（包装类）查询用户
    public void fun2() throws IOException {

        // 加载主配置文件
        InputStream resource = Resources.getResourceAsStream("mybatis.xml");
        // 获得mybatis的sessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        // 获得session
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 获得user的mapper对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        QueryVo vo = new QueryVo();
        User user = new User();
        user.setUsername("王");
        vo.setUser(user);
        List<User> userByQueryVo = mapper.findUserByQueryVo(vo);
        for (User u : userByQueryVo) {

            System.out.println(u);
        }
    }

    @Test
    // 测试mapper查询用户个数
    public void fun3() throws IOException {

        // 加载主配置文件
        InputStream resource = Resources.getResourceAsStream("mybatis.xml");
        // 获得mybatis的sessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        // 获得session
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 获得user的mapper对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Integer userCount = mapper.findUserCount();
        System.out.println(userCount);
    }

    @Test
    // 测试mapper中resultMap属性
    public void fun4() throws IOException {

        // 加载主配置文件
        InputStream resource = Resources.getResourceAsStream("mybatis.xml");
        // 获得mybatis的sessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        // 获得session
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 获得user的mapper对象
        OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class);
        List<Orders> allOrsers = mapper.findAllOrsers();
        for (Orders o : allOrsers) {

            System.out.println(o);
        }
    }

    @Test
    // <!-- 按照性别和名称查询用户-->
    public void fun5() throws IOException {

        // 加载主配置文件
        InputStream resource = Resources.getResourceAsStream("mybatis.xml");
        // 获得mybatis的sessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        // 获得session
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 获得user的mapper对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        //        user.setSex("1");
        user.setUsername("张小明");
        List<User> users = mapper.findUserBySexAndName(user);
        for (User u : users) {
            System.out.println(u);
        }
    }

    @Test
    // 测试<foreach>标签  查询多个id(集合对象)
    public void fun6() throws IOException {
        // 加载主配置文件
        InputStream resource = Resources.getResourceAsStream("mybatis.xml");
        // 获得mybatis的sessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        // 获得session
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 获得user的mapper对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(10);
        ids.add(16);
        ids.add(22);
        QueryVo vo = new QueryVo();
        vo.setIds(ids);
        List<User> users = mapper.findUserListByIds(vo);
        for (User u : users) {
            System.out.println(u);
        }
    }

    @Test
    // 测试<foreach>标签  查询多个id（数组对象）
    public void fun7() throws IOException {
        // 加载主配置文件
        InputStream resource = Resources.getResourceAsStream("mybatis.xml");
        // 获得mybatis的sessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        // 获得session
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 获得user的mapper对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Integer[] ids = {10, 26};
        QueryVo vo = new QueryVo();
        vo.setIdss(ids);
        List<User> users = mapper.findUserByIds(vo);
        for (User u : users) {
            System.out.println(u);
        }
    }

    @Test
    // 测试<foreach>标签  查询多个id（传入数组为参数）
    public void fun8() throws IOException {
        // 加载主配置文件
        InputStream resource = Resources.getResourceAsStream("mybatis.xml");
        // 获得mybatis的sessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        // 获得session
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 获得user的mapper对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Integer[] ids = new Integer[3];
        ids[0] = 10;
        ids[1] = 16;
        ids[2] = 26;
        List<User> users = mapper.findUserByIdsssss(ids);
        for (User u : users) {
            System.out.println(u);
        }
    }
}
